
export function getQueryByKey(key: string): any {
  let data: { [prop: string]: any } = {};

  let src: string = window.location.href;
  let index = src.indexOf("?");
  if (index === -1) return null;
  let dataStr = src.substring(src.indexOf("?") + 1);
  let dataArray = dataStr.split("&");
  for (let i = 0; i < dataArray.length; i++) {
    let param = dataArray[i].split("=");
    data[param[0]] = param[1];
  }
  return data[key];
}


export function sleep(time: number) {
  return new Promise((resolve: any) => setTimeout(resolve, time))
}

export function getDate(distance = 0) {
  const curDate = new Date();
  const year = `${curDate.getFullYear() - distance}`.padStart(2, "0");
  const month = `${curDate.getMonth() * 1 + 1}`.padStart(2, "0");
  const day = `${curDate.getDate()}`.padStart(2, "0");

  return `${year}-${month}-${day}`;
}

export function getFormatDate() {
  const curDate = new Date();
  const year = `${curDate.getFullYear()}`.padStart(2, "0");
  const month = `${curDate.getMonth() * 1 + 1}`.padStart(2, "0");
  const day = `${curDate.getDate()}`.padStart(2, "0");
  let hour = `${curDate.getHours()}`.padStart(2, "0");
  let minute = `${curDate.getMinutes()}`.padStart(2, "0");
  let second = `${curDate.getSeconds()}`.padStart(2, "0");

  return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}

export function getSystem() {
  if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) return 'iOS'
  return 'android'
}
